package csv

import sugarbind_input.*
import app.*

class CsvParserBibliography {

	static int ARTICLE_IMPORT_REF = 0
	static int TITLE = 1
	static int AUTHORS = 2
	static int PUB_YEAR = 3
	static int CITATION = 4
	static int URL = 5
	static int PM_ID = 6
	static int ARTICLE_TYPE_NAME = 7
	
	static int LAST_NAME = 0
	static int FIRST_NAME = 1
	static int MID_NAME = 2
	static int PERSON_TITLE = 3
	static int SUFFIX = 4

	def parse
	
	def idx=0
	
	ObjFinder finder = new ObjFinder() 
	DefaultValue d = new DefaultValue()
	
	
	CsvParserBibliography(String filepath)
	{
		println("CsvParser : Bibliography" + filepath)
			
		parse = new File(filepath).readLines()[1..-1]
		csvParseBib()		
	}
	
	public CsvParserBibliography()
	{		
	}
	
	def csvParseBib()
	{
//		 def start = System.currentTimeMillis()
//		 csvParseArticleType()
//		 csvParseArticle()
//		 csvParseAuthor()
//		 csvParseAuthorArticle()
//		 def end = System.currentTimeMillis()
//		 println("Processing time : " + (end-start)/1000 + "[s]")
		 
		 /*----------------------------------------*/
		 def start = System.currentTimeMillis()
		 parse.each {line ->
			 idx++
			 println("====================")
			 println("Row $idx")
			 println("$line")
			 println("====================")
			 
			 line.splitEachLine(d.tokenSemiColon) { fields ->
				 csvParseArticleType(fields[ARTICLE_TYPE_NAME])
				 csvParseArticle(fields[ARTICLE_IMPORT_REF], fields[TITLE], fields[PUB_YEAR], fields[CITATION], fields[URL], fields[PM_ID], finder.findArticleType(fields[ARTICLE_TYPE_NAME]))		 
				 csvParseAuthors(fields[AUTHORS], true)
				 csvParseAuthorArticle(fields[AUTHORS], finder.findArticle(fields[TITLE], fields[PUB_YEAR]))
			 }
		 }
		 def end = System.currentTimeMillis()
		 
		 println("Processing time : " + (end-start)/1000 + "[s]")
	}
	
	//-------------------
	//Parsers
	//-------------------
	def csvParseArticleType(String articleTypeName)
	{
		def obj = new Article_Type(
			article_Type_Name: Utils.importFormat(articleTypeName)
			)
		obj.validate()
		obj.save(flush: true)
	}
	
	def csvParseArticle(String articleImportRef, String title, String pubYear, String citation, String url, String pmId, Article_Type articleType)
	{
		def obj = new Article(
			article_Import_Ref: Utils.importFormat(articleImportRef),
			title: Utils.importFormat(title),
			pub_Year: Utils.importFormat(pubYear),
			citation:Utils.importFormat(citation),
			url:Utils.importFormat(url),
			pm_Id:Utils.importFormat(pmId),
			article_Type: articleType
			)
		obj.validate()
		obj.save(flush: true)
	}
	
	def csvParseAuthors(String authorsLine, boolean saveObj)
	{
		int arrayWidth = SUFFIX + 1 //FIRST_NAME
		
//		println("Authors : $authorsLine ")
		def authors = (Utils.importFormat(authorsLine)).split(d.semicolon)
//		println("Authors : $authors ")
		
		String[][] authorsArray = new String[authors.length][arrayWidth]
			println("Authors : $authors ")
			
			def authorData = new String[5];
//			println("authors length : ${authors.length} ")
			for (int i = 0; i<authors.length ;i++) 
			{
				def data = authors[i].split(d.comma, arrayWidth)
				
				for(int j = 0; j<data.length ;j++)
				{
					authorData[j] = data[j]
					authorsArray[i][j]=authorData[j]
				}
			
				if(saveObj)
				{
					csvParseAuthor(authorData[LAST_NAME], authorData[FIRST_NAME], authorData[MID_NAME], authorData[PERSON_TITLE], authorData[SUFFIX])
//					csvParseAuthor(authorData[LAST_NAME], authorData[FIRST_NAME])
				}
			}
			return authorsArray		
	}
	
	def csvParseAuthor(String lastName, String firstName)
	{
		def obj = new Author(
			last_Name: Utils.importFormat(lastName),
			first_Name: Utils.importFormat(firstName)
		)
		obj.validate()
		obj.save(flush: true)
	}
	
	def csvParseAuthor(String lastName, String firstName, String midName, String title, String suffix)
	{
		def obj = new Author(
			last_Name: Utils.importFormat(lastName),
			first_Name: Utils.importFormat(firstName),
			mid_Name: Utils.importFormat(midName),
			title: Utils.importFormat(title),
			suffix: Utils.importFormat(suffix)
		)
		obj.validate()
		obj.save(flush: true)
	}

	def csvParseAuthorArticle(String authorsLine, Article article)
	{
		def authors = csvParseAuthors(authorsLine, false)
		def primary, last
		
		for(int i=0; i<authors.length; i++)
		{
			if(i==0){primary = true}else{primary = false}
			if(i==authors.length-1){last = true}else{last = false}
				
			def author
			def midName = authors[i][MID_NAME]
			
			//if(midName == "" || midName == null)
			if(midName == null)
			{
				author = finder.findAuthor(authors[i][LAST_NAME], authors[i][FIRST_NAME], null)
			}
			else
			{
				author = finder.findAuthor(authors[i][LAST_NAME], authors[i][FIRST_NAME], authors[i][MID_NAME])
			}
			
			def obj = new Author_Article(
				article: article,
				author: author,	
				author_Rank: i, 
				is_Primary: primary,
				is_Last: last
			)
			obj.validate()
			obj.save(flush: true)
		}
			
	}
	
	public def csvParseAuthorArticle(HashSet<Author> authors, Article article)
	{
		
		for(Author author : authors)
		{
			def obj = new Author_Article(
				article: article,
				author: author,
				author_Rank: 0,
				is_Primary: false,
				is_Last: false
			)
			obj.validate()
			obj.save(flush: true)
		}
			
	}
	
}
